import { http } from "@/utils/http";

// 业主查询参数接口
export interface OwnerQueryParams {
  page: number;
  limit: number;
  ownerName?: string;
  phone?: string;
  status?: number;
  cellId?: number;
  houseId?: number;
}

// 业主信息接口
export interface Owner {
  id: number;
  ownerName: string;
  name?: string; // 详情接口返回的字段名
  phone: string;
  status: number;
  cellId: number;
  cellName: string;
  houseId: number;
  houseNumber: string;
  buildingName: string;
  unitName: string;
  number: string;
  residentType: number;
  email?: string;
  gender?: number;
  idCard?: string;
  moveInTime: string;
  moveOutTime?: string;
  createdTime?: string;
  updatedTime: string;
  remark?: string;
}

// 创建业主请求接口
export interface CreateOwnerRequest {
  cellId: number;
  name: string;
  phone: string;
  gender: number;
  email: string;
  remark: string;
  idCard: string;
  moveInTime: string;
  houseId: number;
  residentType: number;
}

// 更新业主请求接口
export interface UpdateOwnerRequest {
  id: number;
  cellId: number;
  name: string;
  phone: string;
  gender: number;
  email: string;
  remark: string;
  idCard: string;
  moveInTime: string;
  residentType: number;
}

// 业主列表响应接口
export interface OwnerListResponse {
  code: number;
  message: string;
  data: {
    list: Owner[];
    total: number;
    page: number;
    limit: number;
  };
}

// 业主详情响应接口
export interface OwnerDetailResponse {
  code: number;
  message: string;
  data: Owner;
}

// 业主迁出请求接口
export interface OwnerMoveOutRequest {
  houseId: number;
}

// 批量业主迁出请求接口
export interface BatchOwnerMoveOutRequest {
  houseIds: number[];
}

// 业主迁入请求接口
export interface OwnerMoveInRequest {
  ownerId: number;
  houseId: number;
  moveInTime: string;
}

// 批量审核请求接口
export interface BatchApprovalRequest {
  ownerIds: number[];
}

// 通用响应接口
export interface ApiResponse {
  code: number;
  message: string;
  data?: any;
}

/**
 * 获取业主列表
 */
export const getOwnerList = (
  params: OwnerQueryParams
): Promise<OwnerListResponse> => {
  return http.request("get", "/owner/list", { params });
};

/**
 * 根据ID获取业主详情
 */
export const getOwnerById = (ownerId: number): Promise<OwnerDetailResponse> => {
  return http.request("get", "/owner/detail", { params: { ownerId } });
};

/**
 * 创建业主
 */
export const createOwner = (data: CreateOwnerRequest): Promise<ApiResponse> => {
  return http.request("post", "/owner/add", { data });
};

/**
 * 更新业主
 */
export const updateOwner = (data: UpdateOwnerRequest): Promise<ApiResponse> => {
  return http.request("post", "/owner/update", { data });
};

/**
 * 删除业主
 */
export const deleteOwner = (ownerId: number): Promise<ApiResponse> => {
  return http.request("get", "/owner/delete", { params: { ownerId } });
};

/**
 * 批量删除业主
 */
export const batchDeleteOwner = (ownerIds: number[]): Promise<ApiResponse> => {
  return http.request("post", "/owner/deleteBatch", { data: ownerIds });
};

/**
 * 业主迁入
 */
export const moveInOwner = (data: OwnerMoveInRequest): Promise<ApiResponse> => {
  return http.request("post", "/owner/moveIn", { data });
};

/**
 * 业主迁出
 */
export const moveOutOwner = (houseId: number): Promise<ApiResponse> => {
  return http.request("post", "/owner/moveOut", { data: houseId });
};

/**
 * 批量业主迁出
 */
export const batchMoveOutOwner = (houseIds: number[]): Promise<ApiResponse> => {
  return http.request("post", "/owner/moveOutBatch", { data: houseIds });
};

/**
 * 批量通过审核
 */
export const batchApproveOwner = (ownerIds: number[]): Promise<ApiResponse> => {
  return http.request("post", "/owner/approvedBatch", { data: ownerIds });
};

/**
 * 批量不通过审核
 */
export const batchDisapproveOwner = (
  ownerIds: number[]
): Promise<ApiResponse> => {
  return http.request("post", "/owner/disapprovedBatch", { data: ownerIds });
};

/**
 * 导入业主
 */
export const importOwners = (file: File): Promise<ApiResponse> => {
  const formData = new FormData();
  formData.append("file", file);

  return http.request("post", "/owner/import", {
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/**
 * 导出业主模板
 */
export const exportOwnerTemplate = (): Promise<Blob> => {
  return http.request("get", "/owner/exportTemplate", { responseType: "blob" });
};

/**
 * 导出业主数据
 */
export const exportOwnerData = (params: OwnerQueryParams): Promise<Blob> => {
  return http.request("get", "/owner/export", { params, responseType: "blob" });
};

/**
 * 根据业主名字模糊查询获取业主ID列表
 */
export const searchOwnerByName = (ownerName: string): Promise<ApiResponse> => {
  return http.request("get", "/owner/searchByName", {
    params: { ownerName }
  });
};
